<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 17-4-25
 * Time: 下午2:42
 */

namespace Admin\Model;

use Think\Model;
class ProdcatesModel extends Model{


    public function catTree(){
        $data=$this->select();
        return $this->reSort($data);
    }
//递归函数
    public  function reSort($data,$pid=0,$level=0){
        static $ret=array();
        foreach($data as $k=>$v){
            if($v['pid'] == $pid){
                $v['level'] = $level;
                $ret[]=$v;
                $this->reSort($data,$v['id'],$level+1);
            }
        }
        return $ret;
    }

    protected function _before_delete($options){

        $nmodel=M('Goods');
        if(is_array($options['where']['id']))
            $nmodel->where("gcats IN({$options['where']['id'][1]})")->delete();
        else
            $nmodel->where('gcats='.$options['where']['id'])->delete();
    }


} 